package com.hc.common.utils;

import com.hc.domain.User;
import com.hc.query.SearchVO;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * 控制器工具类
 *
 * @author hecai
 */
public class ControllerUtil {
    /**
     * 调用obj的set方法将searchVO的值设置到obj中
     *
     * @param obj
     * @param searchVO 查询条件
     * @param <T>
     */
    public static <T> void initParam(T obj, SearchVO searchVO) {
        if (searchVO != null) {
            String searchKey = searchVO.getSearchKey();
            String searchValue = searchVO.getSearchValue();
            if (searchKey != null && searchKey.length() > 0 && searchValue != null && searchValue.length() > 0) {
                try {
                    Method method = obj.getClass().getMethod("set" + StringUtil.firstLatterUpper(searchKey), String.class);
                    method.invoke(obj, searchValue);
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        User user = new User();
        String searchKey = "tel";
        String searchValue = "432423";
        SearchVO searchVO = new SearchVO(searchKey,searchValue);
        initParam(user, searchVO);
        System.out.println(user);
    }
}
